package com.cqq.pro.upload;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqq.common.core.base.BaseController;
import com.cqq.common.core.bean.MessageBean;
import com.cqq.common.core.util.GuidUtil;
import com.cqq.pro.util.DownOrUpLoadUtil;
import com.cqq.pro.util.PropertiesUtil;

@Controller
@RequestMapping("/imageUpload")
public class ImageUploadController extends BaseController{

	public static final String FILE_PATH = PropertiesUtil.getConfigValue("file_path");
	
	@RequestMapping(value = { "uploadPictures" })
	@ResponseBody
    public MessageBean uploadPicturesOfFeedback(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        log.info("in upload........begin.......");
        DiskFileItemFactory fac = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setHeaderEncoding("UTF-8");
        List fileList = null;
        try {
                fileList = upload.parseRequest(request);
        } catch (FileUploadException ex) {
        	log.error(ex.getMessage(), ex);
            return buildMessage(MESSAGE_TYPE_ERROR, "上传失败【接收图片】");
        }
        
        Iterator<FileItem> it = fileList.iterator();
        List<String> fileNames = new ArrayList<String>();
        while (it.hasNext()) {
                FileItem item = it.next();
                if (!item.isFormField()) {
                        String name = item.getName();
                        if (name == null || name.trim().equals("")) {
                                continue;
                        }
                        try {
                        	    log.info("in upload........begin to save picture......."+FILE_PATH);
                                
                        	    String fileName = item.getName();
                                fileName = GuidUtil.generate()+fileName.substring(fileName.lastIndexOf('.'));
                                
                                DownOrUpLoadUtil.saveFile(FILE_PATH, fileName, item);
                                fileNames.add(fileName);
                                log.info("in upload........begin to save picture.......");
                        } catch (Exception e) {
                        	log.error(e.getMessage(), e);
                            return buildMessage(MESSAGE_TYPE_ERROR, "保存反馈失败【保存图片】");
                        }
                }
        }     
        return buildMessage(MESSAGE_TYPE_SUCCESS,FILE_PATH, "上传成功",fileNames);
    }
	
}
